package com.shd.edanpei.presenter;

import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.ApiParams;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.presenter.contract.UserInfoContract;

import java.io.File;
import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by mc on 2018/9/6.
 * yang-wen-wen
 */

public class UserInfoPresenter extends BasePresenterImpl<UserInfoContract.UserInfoIView>
        implements UserInfoContract.UserInfoIPresenter {

    public UserInfoPresenter(UserInfoContract.UserInfoIView mView) {
        super(mView);
    }

    @Override
    public void uploadPicture(String picturePath) {
        ApiCreator.getApi().uploadPicture(createFileBody(picturePath), createStrBody("1"))
                .compose(RxTransformer.<String>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onSuccess(String data) {
                        DaoManager.getInstance().setAvatalUrl(data);
                        update(ApiParams.AVATAR_URL, data);
                    }
                });
    }


    public void update(final String key, final Object value) {
        WeakHashMap<String, Object> weakHashMap = new WeakHashMap<>();
        weakHashMap.put(key, value);
        ApiCreator.getApi().updateUserInfo(weakHashMap)
                .compose(RxTransformer.<UserBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UserBean>() {
                    @Override
                    protected void onSuccess(UserBean data) {
                        switch (key) {
                            case ApiParams.AVATAR_URL:
                                DaoManager.getInstance().setAvatalUrl(data.getAvatarUrl());
                                break;
                            case ApiParams.GENDER:
                                DaoManager.getInstance().setGender(data.getSex());
                                break;
                            case ApiParams.BIRTHDAY:
                                DaoManager.getInstance().setBirthday(data.getBirthday());
                                break;
                        }
                    }
                });

    }

    private MultipartBody.Part createFileBody(final String pic) {
        File file = new File(pic);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        return MultipartBody.Part.createFormData("file", file.getName(), requestBody);
    }

    private RequestBody createStrBody(final String value) {
        return RequestBody.create(MediaType.parse("text/plain"), value);
    }
}
